package com.benyi.byiee.rest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.benyi.byiee.constants.Role;
import com.benyi.byiee.constants.UserType;
import com.benyi.byiee.model.api.LoginReq;
import com.benyi.byiee.model.api.User;
import com.benyi.byiee.model.api.VerifyMail;
import com.benyi.byiee.model.db.UserAccount;
import com.benyi.byiee.service.AccountService;
import com.benyi.byiee.util.OnlineUserList;
import com.benyi.util.JsonResult;
import com.benyi.util.bean.ServiceException;



@RestController
@RequestMapping("/u")
public class AccountRest {
	private static Logger log = LoggerFactory.getLogger(AccountRest.class);
	
	@Autowired
	private AccountService service;
	
	
	@RequestMapping(value = "/in", method = RequestMethod.POST,consumes = {"application/json"})
	public JsonResult<UserAccount> login(@RequestBody LoginReq req){ 
		JsonResult<UserAccount> res ;
		try { 
			UserAccount u = service.login(req.getEmail(), req.getPasswd());  
			res = JsonResult.success(u,"welcome"); 
		}catch(ServiceException e) {
			res =JsonResult.fail(e.getErrorCode() , e.getMessage());
		}
		return res;
	}
	
	//@Role(value=UserType.ALL)  
	@RequestMapping(value = "/out", method = RequestMethod.POST,consumes = {"application/json"})
	public JsonResult<UserAccount> logout(@RequestHeader String token){ 
		JsonResult<UserAccount> res ;
		UserAccount u = service.logout(token);
		if(u==null) {
			res = JsonResult.fail(null);
		}else {  
			u.setToken(null);
			res = JsonResult.success(u,"welcome");
		}
		return res;
	}
	
	@Role(value=UserType.ALL)  
	@RequestMapping(value = "/beat", method = RequestMethod.POST,consumes = {"application/json"})
	public JsonResult<UserAccount> heartBeat(@RequestHeader String token){ 
		JsonResult<UserAccount> res ;
		UserAccount u = service.heartBeat(token);
		if(u==null) {
			res = JsonResult.fail(null);
		}else { 
			res = JsonResult.success(u,"suceed");
		}
		return res;
	}
	
	@RequestMapping(value = "/reg", method = RequestMethod.POST,consumes = {"application/json"})
	public JsonResult<UserAccount> register(@RequestBody LoginReq req){ 
		JsonResult<UserAccount> res ;
		try {
			UserAccount u = service.register(req.getEmail(), req.getPasswd(),req.getLang());
			u.setRid(null);
			res = JsonResult.success(u,"welcome");
		}catch(ServiceException e) {
			res =JsonResult.fail(e.getErrorCode() , e.getMessage());
		} 
		return res;
	}
	
	@RequestMapping(value = "/setPwd", method = RequestMethod.POST,consumes = {"application/json"})
	public JsonResult<UserAccount> setPassword(@RequestBody LoginReq req){ 
		JsonResult<UserAccount> res ;
		try {
			UserAccount u = service.resetPassword(req.getEmail(), req.getPasswd(),req.getCode());
			res = JsonResult.success(u,"success");
		}catch(ServiceException e) {
			res =JsonResult.fail(e.getErrorCode() , e.getMessage());
		}  
		return res;
	}
	
	@RequestMapping(value = "/atv", method = RequestMethod.POST,consumes = {"application/json"})
	public JsonResult<UserAccount> activeAccount(@RequestBody LoginReq req){ 
		JsonResult<UserAccount> res ;
		try {
			UserAccount u = service.active(req.getEmail(), req.getCode(),req.getPasswd());
			res = JsonResult.success(u,"success");
		}catch(ServiceException e) {
			res =JsonResult.fail(e.getErrorCode() , e.getMessage());
		}  
		return res;
	}
	
	@RequestMapping(value = "/sActive", method = RequestMethod.POST,consumes = {"application/json"})
	public JsonResult<UserAccount> activeAccount(@RequestBody VerifyMail req){ 
		JsonResult<UserAccount> res ;
		int v = service.sendActiveMail(req.getMail(),req.getLang());
		if(v < 0) {
			res = JsonResult.fail(null);
		}else {
			res = JsonResult.success("sucess");
		}
		return res;
	}
	
	@RequestMapping(value = "/sVerify", method = RequestMethod.POST,consumes = {"application/json"})
	public JsonResult<UserAccount> sendVerifyCode(@RequestBody VerifyMail req){ 
		JsonResult<UserAccount> res ;
		int v = service.sendVerifyCode(req.getMail(),req.getLang());
		if(v < 0) {
			res = JsonResult.fail(null);
		}else {
			res = JsonResult.success("sucess");
		}
		return res;
	}
	
	@Role(value=UserType.ALL)  
	@RequestMapping(value = "/svb", method = RequestMethod.POST,consumes = {"application/json"})
	public JsonResult<UserAccount> updateBase(@RequestHeader String token, @RequestBody UserAccount base){ 
		JsonResult<UserAccount> res ;
		User user = OnlineUserList.getUser(token);  
		if(user==null) {
			res = JsonResult.fail("not_login","not login user");
			return res;
		}
		String email = user.getAccount().getEmail();
		if(email == null) {
			email="";
		}
		if( email.equalsIgnoreCase(base.getEmail()) ==false ) {
			res = JsonResult.fail("no_right","not same user");
			return res;
		}
		
		UserAccount u = service.updateBase(user ,  base);
		if(u==null) {
			res = JsonResult.fail(null);
		}else {  
			u.setToken(user.getToken());
			res = JsonResult.success(u,"success");
		}
		return res;
	}
	
}
